/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-08-03     Lenovo       the first version
 */
#ifndef APPLICATIONS_THAISENCHARGMODULE_H_
#define APPLICATIONS_THAISENCHARGMODULE_H_


#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "stm32f4xx_hal.h"
#include "thaisen7102Public.h"
#include "thaisenGunDef.h"
#include "thaisenChargFlow.h"
#include "thaisenChargModuleLib.h"
#include "serialScreen.h"


#define THAISEN_MODULE_GROUP_NUM 2
#define THAISEN_MODULE_SIGNALGROUP_NUMMAX 0x10
#define THAISE_MODULE_GROUP_1   0
#define THAISE_MODULE_GROUP_2   1





typedef void (*thaisenModuleFunT)(void);
typedef void (*thaisenModuleInitFunT)(struct thasienPowerControlSetStruct *PowerInfoSetInfo);
typedef void (*thaisenModuleGet1FunT)(uint8_t);
typedef void (*thaisenModuleGet4FunT0)(uint8_t,uint8_t, uint8_t, uint16_t);
typedef void (*thaisenModuleGet4FunT1)(uint32_t,uint32_t,uint8_t, uint8_t);
typedef uint32_t (*thaisenModuleGet32_1FunT)(uint8_t);
typedef uint8_t (*thaisenModuleFun8)(void);
typedef thaisenRelayUEn (*thaisenModuleRelayU)(void);
typedef void* (*thaisenModuleInfoU)(uint8_t, uint8_t);




typedef enum thaisenModuleErrEnum{
	MODULE_ERR_COMM,		//通讯故障
	MODULE_ERR_OUT_OV,		//输出过压
	MODULE_ERR_SCI,			//SCI通信故障
	MODULE_ERR_INM,			//输入模式检测错误
	MODULE_ERR_TOTAL,		//模块故障
	MODULE_ERR_PROTECT,		//模块保护
	MODULE_ERR_FAN,			//风扇故障
	MODULE_ERR_EQUAL,		//均流故障
	MODULE_ERR_INPUTAC,		//输入故障
	MODULE_ERR_M_SET,		//监控下发与实际不匹配错误
	MODULE_ERR_PFC,			//PFC故障
	MODULE_OUTPUT_SHORT,	///输出短路
	MODULE_ENV_TEMP,		//环境过温
	MODULE_ERR_BALANCE,		//三相不平衡
	MODULE_ERR_RES,			//泄放故障
	MODULE_ERRNUM_MAX,
}thaisenModuleErrEnum;


struct thaisenModuleElectStruct
{
    thaisenModuleInitFunT moduleInit;
    thaisenModuleGet4FunT0 moduleOpenClose;
    thaisenModuleGet4FunT1 moduleSetVoltCurr;
    thaisenModuleFunT moduleAddress;
    thaisenModuleFunT moduleGroup;
    thaisenModuleGet1FunT moduleReadVoltCurr;
    thaisenModuleGet32_1FunT moduleGetVolt;
    thaisenModuleFun8 CC1_Status;
    thaisenModuleRelayU parallelZRelayEnable;
    thaisenModuleRelayU parallelZRelayClose;
    thaisenModuleRelayU parallelFRelayEnable;
    thaisenModuleRelayU parallelFRelayClose;
    thaisenModuleGet32_1FunT AmmeterVolt;
	thaisenModuleFunT chargModuleProcess;
    thaisenModuleFun8 debugMode;
    thaisenModuleGet1FunT moduleReadState;
    thaisenModuleInfoU getModuleState;
    thaisenModuleInfoU getModuleVoltCurr;
    thaisenModuleFunT dcRelayEnableA;
    thaisenModuleFunT dcRelayCloseA;
    thaisenModuleFunT dcRelayEnableB;
    thaisenModuleFunT dcRelayCloseB;
    void (*moduleHeart)(uint8_t);
    void (*set_terminal_status)(uint8_t,uint8_t,uint8_t,uint8_t,uint8_t);
    void (*set_terminal_data)(uint8_t,uint16_t,uint16_t,uint16_t,uint16_t);
};

struct thaisenAlrmStruct{
	uint8_t event;
	uint8_t status;
	uint16_t timer;
};

typedef struct thaisenDataStruct
{
    uint16_t SentCnt;   //模块通讯发送计数
    uint16_t RecvCnt;   //模块通讯接收计数
    uint16_t SentErrCnt;//模块通讯故障发送计数
}thaisenDataSt;

typedef struct thaisenModuleStruct
{
    struct thaisenModuleElectStruct Elect;//选择函数
	struct thaisenAlrmStruct Alrm[MODULE_ERRNUM_MAX];//告警的事件、状态、时间
	struct thaisenDataStruct Data;//数据计数
}thaisenModuleSt;

typedef enum thaisenModuleProEnum
{
	thaisenModuleProYFY,
	thaisenModuleProGW,
	thaisenModuleProWinline,
	thaisenModuleProYouYou,
	thaisenModuleProYiNeng,
	thaisenModulePowerControl,
}thaisenModuleProEnum;

typedef enum thaisenModuleStatusEnum
{
    thaisenModuleIdle,
    thaisenModuleChargInsult,
    thaisenModuleChargCRO,
    thaisenModuleChargCCS,
    thaisenModuleChargStop,
    thaisenModuleChargParallel,
    thaisenModuleChargParallelDischarg,
    thaisenModuleFault,
    thaisenModuleAll,
}thaisenModuleStatusEn;

extern struct rt_messagequeue thaisenModuleMq;
extern can_msg_buf thaisenModuleMsgPool[200];

extern thaisenModuleGetStatusFun_p  thaisenModuleGetStatusFun;
extern struct thaisenBMS_Charger_struct *thaisenBMS_Add[THAISEN_GUN_NUM];

void thaisen_clear_SentErrCnt(uint8_t gunNum);
uint16_t thaisen_get_SentErrCnt(uint8_t gunNum);

uint8_t thaisen_get_chargModule_debug_modeA(void);
uint8_t thaisen_get_chargModule_debug_modeB(void);


uint16_t  thaisenModuleGetStatusFun1(uint8_t cmd);
uint8_t thaisenModuleGetStatus(uint8_t gunNum);
void thaisen_chargpowercontrol_Init(thaisenModuleGetStatusFun_p getStaFun,struct thaisenBMS_Charger_struct *chargerA,struct thaisenBMS_Charger_struct *chargerB,struct thasienPowerControlSetStruct *powerS );

#endif /* APPLICATIONS_THAISENCHARGMODULE_H_ */
